<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				relateUserCache:'obj-w-weekreport-title17-relateUser-cache',
				userCacheKey:'obj-w-weekreport-title17-user-cache',
				defaultDataCache1:'obj-w-weekreport-defaultData-user-cache'
			})
		},
		methods:{
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						"dictZero/getListByCode",
						{
							code:'36748'
						}
					),
					_that.om.request(
						"tsysdict/getCombobox",
						{
							code:'38965'
						}
					)
					
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title17","stateCode","data",configSelectData(res[0],'code','dicname'))
					_that.$refs.autoform.changeEditor("title17","cateId","data",configSelectData(res[1],'code','dicname'))
					_that.$refs.autoform.initSelectIndex("title17","stateCode")
					_that.$refs.autoform.initSelectIndex("title17","cateId")
				})
			},
			
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor("title17","relateUserId",{
								"link":'../../public/searchUser/searchUser',
								"linkParam": {
									cachename: _that.relateUserCache,
									isSingle:true
								}
							},null,_that.autoformdata)
							changeEditor("title17","relateUserId",{
								"link":'../../public/searchUser/searchUser',
								"linkParam": {
									cachename: _that.relateUserCache,
									isSingle:true
								}
							},null,_that.autoformdata)
							changeEditor('title17', 'userIds', {
								'link': '../../public/searchUser/searchUser',
								linkParam: {
									namefield: urlUtil.userPageUrl().namefield,
									searchfield: "q",
									defaultDataCache: _that.defaultDataCache1,
									cachename: _that.userCacheKey,
									isNew: true,
									placeholder: urlUtil.userPageUrl().prompt,
									pagination: true
								}
							}, null, _that.autoformdata)
							_that.initload()
							if(_that.formdata.userIds){
								var list2 = []
								let userIds = _that.formdata.userIds.split(',')
								let userNames = _that.formdata.userNames.split(',')
								for (let user5Index in userIds) {
									let obj = {
										id: userIds[user5Index],
										username: userNames[user5Index],
									}
									list2.push(obj)
								}
								uni.setStorage({
									key: _that.defaultDataCache1,
									data: list2
								})
							}
						}
					})
				}
			})
		},
		onUnload() {
			let _that = this
			uni.removeStorage({
				key: _that.defaultDataCache1
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.relateUserCache,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.relateUserId = d.id
							formdata.relateUserName = d.username
							uni.removeStorage({
								key:_that.relateUserCache
							})
						}
					}
				});
				uni.getStorage({
					key: _that.userCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							uni.setStorage({
								key: _that.defaultDataCache1,
								data: d
							})
							var formdata = _that.$refs.autoform.formdata;
							let idList = []
							let nameList = []
							let userIds = ''
							if (formdata.userIds) {
								userIds = formdata.userIds.split(',')
							}
							for (let index in d) {
								idList.push(d[index].id)
								nameList.push(d[index].username)
							}
							let idStr = idList.map(e => e).join(",")
							let nameStr = nameList.map(e => e).join(",");
							formdata.userIds = idStr
							formdata.userNames = nameStr
							uni.removeStorage({
								key: _that.userCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>
</style>
